﻿using Rex.Assignment.Common;
using Rex.Assignment.Domain.DomainService;
using Rex.Assignment.Domain.Models.Entitys;
using Rex.Assignment.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Rex.Assignment.Infrastructure.DomainService
{
    /// <summary>
    /// 登录领域服务
    /// </summary>
    /// <remarks>
    /// @创 建 者：Rex
    /// @创建日期：2021/2/21 15:02:39
    /// </remarks>
    public class LoginDomainService : ILoginDomainService
    {
        private readonly ISysUserRepository _sysUserRepository;

        public LoginDomainService(ISysUserRepository sysUserRepository)
        {
            _sysUserRepository = sysUserRepository;
        }

        /// <summary>
        /// 验证登录
        /// </summary>
        /// <param name="loginId">登录账号</param>
        /// <param name="loginPwd">登录密码</param>
        /// <returns></returns>
        public DataResult<SysUser> VerificationLogin(string loginId, string loginPwd)
        {
            DataResult<SysUser> dr = new DataResult<SysUser>();
            IList<SysUser> sysUserList = _sysUserRepository.GetModels<DateTime>(false, c => c.CreateDate, c => c.LoginID == loginId && c.LoginPwd == loginPwd).ToList();
            dr.Code = sysUserList.Count == 1 ? StatusCode.Succees : StatusCode.Failed;
            dr.Msg = sysUserList.Count == 1 ? "登录成功！" : "登录失败！";
            dr.Data = sysUserList.Count == 1 ? sysUserList.FirstOrDefault() : null;
            dr.Count = sysUserList.Count;
            return dr;
        }
    }
}
